python3_函数

python函数。

python函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3
# -*- coding: utf-8 -*-

print("function practice-------------")
print(abs(100),abs(-100),abs(12.34))
print(max(1,2,3,-2,3))
print(max(range(9)))
a=abs
print(a(-9))
n1=255
print(str(hex(n1)))

print("define function-------------")
def my_abs(x):
if x>=0:
return x
else:
return -x
print(my_abs(-99))
#from abstest import my_abs
# abstest : filename
# my_abs : function name

print("pass-------------")
def nop():
pass

age=22
if age >=18:
pass
#pass nothing to do

print("type checking-------------")
def my_abs2(x):
if not isinstance(x,(int,float)):
raise TypeError("bad operand type")
if x>=0:
return x
else:
return -x

print("return serveral ele-------------")
import math
def move(x,y,step,angle=0):
nx=x+step*math.cos(angle)
ny=y-step*math.sin(angle)
return nx,ny

x,y=move(100,100,60,math.pi/6)
#其实这里的返回值是一个tuple
print(x,y)

print("practice-------------")
import math
def quadratic(a,b,c):
x1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
x2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
return x1,x2
#test
print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4))

if quadratic(2, 3, 1) != (-0.5, -1.0):
print('测试失败')
elif quadratic(1, 3, -4) != (1.0, -4.0):
print('测试失败')
else:
print('测试成功')


print("可变参数-------------")
def calc(*numbers):
sum=0
for n in numbers:
sum = sum + n * n
return sum
#numbers前加*表示将参数作为可变参数传入
nums=[1,2,3]
print(calc(1,2,3,4)) #可变个参数传入
print(calc(*nums)) #将nums作为可变参数传入

print("关键字参数-------------")
def person(name,age,**kw):
print("name:",name," age:",age," others:",kw)
# kw前加**表示这是个关键字参数,也就是键值对,它的长度可以是0-任意个
person("bob",33)
person("john",33,city="beijing")
extra={"cith":"beijing","job":"engineer"}
person("daming",33,**extra)

print("命名关键字参数-------------")
#关键字参数,调用者可以传入任意关键字参数,但有时候希望限定关键字的名字
def person2(name,age,*,city,job):
print(name,age,city,job);
person2("jack",24,city="beijing",job="engineer")

print("参数组合-------------")
'''
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)
在函数调用的时候,Python解释器自动按照参数位置和参数名把对应的参数传进去。
>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, c=3)
a = 1 b = 2 c = 3 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b')
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
>>> f2(1, 2, d=99, ext=None)
a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}
最神奇的是通过一个tuple和dict,你也可以调用上述函数:
>>> args = (1, 2, 3, 4)
>>> kw = {'d': 99, 'x': '#'}
>>> f1(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'d': 99, 'x': '#'}
>>> args = (1, 2, 3)
>>> kw = {'d': 88, 'x': '#'}
>>> f2(*args, **kw)
a = 1 b = 2 c = 3 d = 88 kw = {'x': '#'}
所以,对于任意函数,都可以通过类似func(*args, **kw)的形式调用它,无论它的参数是如何定义的。
'''

print("汉诺塔递归-------------")
#将a上的n个盘子,借助b放到c上
def hannuota(n,a,b,c):
if n==1:
print(a," --> ",c)
else:
hannuota(n-1,a,c,b)
print(a," --> ",c)
hannuota(n-1,b,a,c)
hannuota(3,"A","B","C")

参考

欢迎与我分享你的看法。
转载请注明出处:http://taowusheng.cn/